package com.shop.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.UUID;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils {
	

	private static DataSource dataSource = new ComboPooledDataSource();

	public static DataSource getDataSource() {
		return dataSource;
	}

	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public static void  close(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (st != null) {
				st.close();
				st = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Object getProductID() {
		String uuid = UUID.randomUUID().toString();
		int hascode = Math.abs(uuid.hashCode());
		return "pro" + new Date().getTime() + hascode;
	}
	public static Object getOrderID() {
		String uuid = UUID.randomUUID().toString();
		int hascode = Math.abs(uuid.hashCode());
		return "order" + new Date().getTime() + hascode;
	}
}
